from sqlalchemy.orm import Session
from ..models.module import Module

# 管理员可访问的模块列表
def get_initial_modules():
    return [
        # 系统管理根模块
        {
            "name": "系统管理",
            "code": "system_management",
            "description": "系统核心管理功能",
            "icon": "fa-cog",
            "route": "/system",
            "parent_id": None,
            "order": 1
        },
        # 用户管理
        {
            "name": "用户管理",
            "code": "user_management",
            "description": "管理系统用户",
            "icon": "fa-users",
            "route": "/users",
            "parent_id": None,  # 顶级模块
            "order": 2
        },
        {
            "name": "用户列表",
            "code": "user_list",
            "description": "查看所有用户",
            "icon": "fa-list",
            "route": "/users/list",
            "parent_id": None,  # 将在代码中设置为user_management的子模块
            "order": 1
        },
        {
            "name": "添加用户",
            "code": "add_user",
            "description": "创建新用户",
            "icon": "fa-user-plus",
            "route": "/users/add",
            "parent_id": None,  # 将在代码中设置为user_management的子模块
            "order": 2
        },
        # 部门管理
        {
            "name": "部门管理",
            "code": "department_management",
            "description": "管理公司部门",
            "icon": "fa-sitemap",
            "route": "/departments",
            "parent_id": None,  # 顶级模块
            "order": 3
        },
        {
            "name": "部门列表",
            "code": "department_list",
            "description": "查看所有部门",
            "icon": "fa-list",
            "route": "/departments/list",
            "parent_id": None,  # 将在代码中设置为department_management的子模块
            "order": 1
        },
        # 员工管理
        {
            "name": "员工管理",
            "code": "employee_management",
            "description": "管理员工信息",
            "icon": "fa-user-tie",
            "route": "/employees",
            "parent_id": None,  # 顶级模块
            "order": 4
        },
        # 项目管理
        {
            "name": "项目管理",
            "code": "project_management",
            "description": "管理公司项目",
            "icon": "fa-briefcase",
            "route": "/projects",
            "parent_id": None,  # 顶级模块
            "order": 5
        },
        # 任务管理
        {
            "name": "任务管理",
            "code": "task_management",
            "description": "管理项目任务",
            "icon": "fa-tasks",
            "route": "/tasks",
            "parent_id": None,  # 顶级模块
            "order": 6
        },
        # 考勤管理
        {
            "name": "考勤管理",
            "code": "attendance_management",
            "description": "管理员工考勤",
            "icon": "fa-clock",
            "route": "/attendance",
            "parent_id": None,  # 顶级模块
            "order": 7
        },
        # 系统设置
        {
            "name": "系统设置",
            "code": "system_settings",
            "description": "系统配置设置",
            "icon": "fa-sliders-h",
            "route": "/settings",
            "parent_id": None,  # 顶级模块
            "order": 8
        },
        # 数据统计
        {
            "name": "数据统计",
            "code": "data_statistics",
            "description": "系统数据统计分析",
            "icon": "fa-chart-bar",
            "route": "/statistics",
            "parent_id": None,  # 顶级模块
            "order": 9
        },
        {
            "name": "用户统计",
            "code": "user_statistics",
            "description": "用户数据统计",
            "icon": "fa-chart-pie",
            "route": "/statistics/users",
            "parent_id": None,  # 将在代码中设置为data_statistics的子模块
            "order": 1
        },
        {
            "name": "项目统计",
            "code": "project_statistics",
            "description": "项目数据统计",
            "icon": "fa-chart-line",
            "route": "/statistics/projects",
            "parent_id": None,  # 将在代码中设置为data_statistics的子模块
            "order": 2
        },
        {
            "name": "任务统计",
            "code": "task_statistics",
            "description": "任务数据统计",
            "icon": "fa-chart-area",
            "route": "/statistics/tasks",
            "parent_id": None,  # 将在代码中设置为data_statistics的子模块
            "order": 3
        },
        {
            "name": "考勤统计",
            "code": "attendance_statistics",
            "description": "考勤数据统计",
            "icon": "fa-calendar-check",
            "route": "/statistics/attendance",
            "parent_id": None,  # 将在代码中设置为data_statistics的子模块
            "order": 4
        }
    ]

def init_modules(db: Session):
    """初始化模块数据"""
    modules_data = get_initial_modules()
    module_map = {}
    
    # 首先创建顶级模块
    for module_data in modules_data:
        if (module_data["parent_id"] is None and 
            module_data["code"] not in ["user_list", "add_user", "department_list", "user_statistics", "project_statistics", "task_statistics", "attendance_statistics"]):
            # 检查模块是否已存在
            existing_module = db.query(Module).filter(Module.code == module_data["code"]).first()
            if not existing_module:
                module = Module(
                    name=module_data["name"],
                    code=module_data["code"],
                    description=module_data["description"],
                    icon=module_data["icon"],
                    route=module_data["route"],
                    parent_id=module_data["parent_id"],
                    order=module_data["order"],
                    is_active=True
                )
                db.add(module)
                db.flush()  # 立即获取ID
                module_map[module.code] = module.id
    
    # 提交顶级模块
    db.commit()
    
    # 然后创建子模块并设置父模块ID
    for module_data in modules_data:
        if module_data["code"] in ["user_list", "add_user", "department_list", "user_statistics", "project_statistics", "task_statistics", "attendance_statistics"]:
            # 设置父模块ID
            if module_data["code"] == "user_list" or module_data["code"] == "add_user":
                module_data["parent_id"] = module_map.get("user_management")
            elif module_data["code"] == "department_list":
                module_data["parent_id"] = module_map.get("department_management")
            elif module_data["code"] == "user_statistics":
                module_data["parent_id"] = module_map.get("data_statistics")
            elif module_data["code"] == "project_statistics":
                module_data["parent_id"] = module_map.get("data_statistics")
            elif module_data["code"] == "task_statistics":
                module_data["parent_id"] = module_map.get("data_statistics")
            elif module_data["code"] == "attendance_statistics":
                module_data["parent_id"] = module_map.get("data_statistics")
            
            # 检查模块是否已存在
            existing_module = db.query(Module).filter(Module.code == module_data["code"]).first()
            if not existing_module:
                module = Module(
                    name=module_data["name"],
                    code=module_data["code"],
                    description=module_data["description"],
                    icon=module_data["icon"],
                    route=module_data["route"],
                    parent_id=module_data["parent_id"],
                    order=module_data["order"],
                    is_active=True
                )
                db.add(module)
    
    # 提交所有子模块
    db.commit()
    
    # 打印初始化结果
    total_modules = db.query(Module).count()
    print(f"模块数据初始化完成，共创建 {total_modules} 个模块")